package avicit.bdp.dcs.job.rest;

import avicit.bdp.dcs.Constant;
import avicit.bdp.dcs.job.dto.JobDTO;
import avicit.bdp.dcs.job.dto.ScheduleTaskDto;
import avicit.bdp.dcs.tools.enums.SyncStatus;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import static org.junit.Assert.assertEquals;

public class JobRestTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void getJobList() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/getJobList?jobType=1", ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void saveDataSource() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setName("junit_test");
        jobDTO.setJobType(1);
        jobDTO.setSrcDatasourceId("1");
        jobDTO.setDstDatasourceId("1");
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/saveDataSource", httpEntity, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void saveTableMapping() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        jobDTO.setTransferType("1,2");
        jobDTO.setProcessMode(0);
        jobDTO.setSelectedTable("[{\"src_table\":\"bdp_dcs_alert\",\"dst_table\":\"bdp_dcs_alert1\"}]");
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/saveTableMapping", httpEntity, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void updateJobStatus() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        jobDTO.setJobStatus(SyncStatus.DRAFT.getCode());
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/updateJobStatus", httpEntity, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void releaseJob() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/releaseJob?jobId="+Constant.JOBID+"&enable=1", ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void startJobByIds() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/startJobByIds?jobIds="+Constant.JOBID, null, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void suspendJobByIds() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/suspendJobByIds?jobIds="+Constant.JOBID, null, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void finishJobByIds() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/finishJobByIds?jobIds="+Constant.JOBID, null, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void deleteJobByIds() {
        RestTemplate restTemplate  = new RestTemplate();
        restTemplate.delete(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/deleteJobByIds?jobIds="+Constant.JOBID, "");
    }

    @Test
    public void copyJobById() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/copyJobById?jobId="+Constant.JOBID+"&name=dcs_test", null, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void getDataSourceByJobId() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/getDataSourceByJobId?jobId="+Constant.JOBID, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void getTableMappingByJobId() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/getTableMappingByJobId?jobId="+Constant.JOBID, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void getDstTableByJobId() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/getDstTableByJobId?jobId="+Constant.JOBID, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void saveTimeTask() {
        ScheduleTaskDto scheduleTaskDto = new ScheduleTaskDto();
        scheduleTaskDto.setStartTime("2020-08-01 12:00:00");
        scheduleTaskDto.setEndTime("\"2020-08-01 13:00:00");
        scheduleTaskDto.setCron("");
        scheduleTaskDto.setFailPolicy(2);
        scheduleTaskDto.setPriority(1);
        scheduleTaskDto.setNoticePolicy(3);
        scheduleTaskDto.setUserIds("100");
        String param = JSONObject.toJSONString(scheduleTaskDto);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/saveTimeTask", null, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void getHistoryJobList() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/getHistoryJobList?jobId="+Constant.JOBID, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void checkTaskConfig() {
        JobDTO jobDTO = new JobDTO();
        jobDTO.setId(Constant.JOBID);
        String param = JSONObject.toJSONString(jobDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/checkTaskConfig?jobId="+Constant.JOBID, null, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void getSelectedTableList() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/job/JobRest/getSelectedTableList?jobId="+Constant.JOBID, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

}
